//
//  AppDelegate.swift
//  BarCode
//
//  Created by xxcao on 2017/3/29.
//  Copyright © 2017年 MobileArtisan. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    var serverSion: String?
    
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //init
        globalInit()
        
        //UI
        if (window == nil) {
            window = UIWindow(frame: UIScreen.main.bounds);
        }
        
        let rootVC = BCLoginViewController();
        window?.rootViewController = rootVC;
        window?.makeKeyAndVisible();
        
        
        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        
    }
    //MARK: -custom func
    func globalInit() {
        Bmob.register(withAppKey: "36ca1e257bc9d1049644b2759b5cdab5")
        
        //      客户端第一次调用 注册用户
//              BmobManager.mockRegister()
        
        BmobManager.checkAppVersion { (res, status) -> (Void) in
            if status == .NRSSuccess {
                serverVersion = res as! String
                self.checkApp(serVersion: res as! String)
            }
        }
    }
    
    func checkApp(serVersion: String) -> Void {
        let alert = CustomAlertView.main()
        let serArr:[String] = serVersion.components(separatedBy: ".")
        let appArr:[String] = appVersion.components(separatedBy: ".")
        if serArr.count == 3 && appArr.count == 3 {
            if serArr[0] != appArr[0] {
                isHasNewVersion = true
                isHasBigVersion = true
                alert?.updateVewsin(true, updateInfo: serVersion)
                return
            }
            if serArr[1] != appArr[1] {
                isHasNewVersion = true
                isHasBigVersion = false
                alert?.updateVewsin(false, updateInfo: serVersion)
                return
            }
            if serArr[2] != appArr[2] {
                isHasNewVersion = true
                isHasBigVersion = false
                alert?.updateVewsin(false, updateInfo: serVersion)
                return
            }
            isHasNewVersion = false
            isHasBigVersion = false
        }
    }
}

